#include <iostream>
#include <string>
#include <memory>

// 1. 定义HAL的抽象基类
class HardwareAbstractionLayer {
public:
    virtual void read() = 0; 					// 定义读取数据的纯虚函数
    virtual void write() = 0; 					// 定义写入数据的纯虚函数
    virtual ~HardwareAbstractionLayer() {} 		// 虚析构函数，保证派生类的正确析构
};

// 2. 创建具体的HAL类，用于不同的硬件平台
class ConcreteHAL1 : public HardwareAbstractionLayer {
public:
    void read() override {
        std::cout << "Reading using ConcreteHAL1." << std::endl;
    }
    void write() override {
        std::cout << "Writing using ConcreteHAL1." << std::endl;
    }
};

class ConcreteHAL2 : public HardwareAbstractionLayer {
public:
    void read() override {
        std::cout << "Reading using ConcreteHAL2." << std::endl;
    }
    void write() override {
        std::cout << "Writing using ConcreteHAL2." << std::endl;
    }
};

// 3. 实现工厂类
class HALFactory {
public:
    static std::unique_ptr<HardwareAbstractionLayer> createHAL(const std::string& type) {
        if (type == "HAL1") {
            return std::make_unique<ConcreteHAL1>();
        } else if (type == "HAL2") {
            return std::make_unique<ConcreteHAL2>();
        } else {
            throw std::runtime_error("Unsupported hardware type");
        }
    }
};

// 4. 示例使用工厂类
int main() {
    // 假设根据环境变量或配置文件得到了硬件类型
    std::string hardwareType = "HAL1"; 		// 这里可以根据实际情况动态设置

    // 使用工厂类创建对应的HAL实例
    auto hal = HALFactory::createHAL(hardwareType);

    // 使用HAL实例进行操作
    hal->read();
    hal->write();

    return 0;
}